package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.UserSign;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.JSONResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/signs")
public class SignController {
    @Autowired
    private ISignService signService;

    @GetMapping()
    public Object signlist(Long userId, Model model){

        Sign sign = signService.selectByPrimaryKey(userId);

        if(sign!=null) {
            List<UserSign> userSigns = sign.getSigntimes();

            List<Map<String, Object>> lists = new ArrayList<>();


            for (UserSign signtime : userSigns) {
                Map<String, Object> map = new HashMap<>();
                map.put("signDay", (signtime.getSigntime().getDate()) + "");
                //System.out.println(signtime.getSigntime().getDate());
                lists.add(map);
            }
            return lists;
        }else {
            List<Map<String, Object>> lists = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("signDay",new Date().getDate());
            lists.add(map);
            return map;
        }
    }



    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Long userId){
        Sign sign = new Sign();
        sign.setUser_id(userId);
        String s = signService.updateByPrimaryKey(sign);
        if (s.length()>0){
            JSONResult jsonResult = new JSONResult();
            jsonResult.mark(s);
            return jsonResult;
        }
        return new JSONResult();
    }

    @GetMapping("/days")
    public Object Sign(Long userId){

        return  signService.selectByPrimaryKey(userId);

    }


    @GetMapping("/integral")
    public Object selectIntByUserId(Long userId){

        return  signService.selectIntByUserId(userId);
    }

}
